const api = require('../../api/api')

Page({
  data: {
    filmId:'',
    film:{},
    actors:[],
    start:0,
    count:10,
    total:0,
    comments:[],
    showNomore:false,
    showLoading:false
  },
  onLoad(options) {
    this.data.filmId = options.filmId
  },
  onReady(){
    this.loadFilmByFilmId()
    this.loadActors()
    this.loadComments()
  },
  loadFilmByFilmId(){
    api.loadFilmByFilmId(this.data.filmId)
      .then(data => {
        this.setData({
          film:data
        })
      })
  },
  loadActors(){
    api.loadActors(this.data.filmId)
      .then(data => {
        this.setData({
          actors: data.items
        })
      })
  },
  loadComments(){
    return api.loadComments(this.data.filmId, {
      start: this.data.start,
      count:this.data.count,
      order_by:'time'
    })
      .then(data => {
        console.log(data)
        this.setData({
          comments: this.data.comments.concat(data.interests),
          start: this.data.start+this.data.count,
          total:data.total
        })
      })
  },
  // 监听上拉加载更多
  onReachBottom(){
    if(this.data.start>this.data.total){
        return this.data({
          showNomore:true
        })
    }
    this.setData({
      showLoading:true
    })
    this.loadComments().then(data => {
      this.setData({
        showLoading:false
      })
    })
  }
})